<?php
namespace app\api\controller;

use app\common\controller\ApiBase;
use Grafika\Grafika;
use Grafika\Color;
use Grafika\Gd\DrawingObject\CubicBezier as GdCubicBezier;
use Grafika\Imagick\DrawingObject\CubicBezier as ImagickCubicBezier;
use app\common\lib\MergePic;
use think\Image;
use app\common\lib\ComposeContent;


class Index extends ApiBase
{

    public function index()
    {
        $content = "最佳";
        $data = checkViolation($content);
        dump($data);
    }



    // public function test(){
    //     $com = apiGetCom('福建省九一六餐饮管理有限公司');
    //     dump($com);
    // }


    public function test222(){
        $cc = new ComposeContent();
        $cc->dalei = '饮品';
        $cc->xiaolei = '奶茶';
        $cc->xiangmu = '一点点奶茶';
        $cc->keywords = '一点点奶茶加盟骗局';
        $cc->keywords_type = '骗局';
        $data = $cc->run();
        dump($data);
    }

    // public function test(){
    //     $input = '/uploads/1111.png';
    //     $input3 = '/uploads/222.png';
    //     $input2 = '/uploads/20200318/137b548c5ece2e8efc3dfa54c0b3f7e1.png';
    //     $text = '夜倾城国际ktv_洪濑店2222';
    //     $text2 = 'shangwuktv.com';
    //     $merge = new MergePic();
    //     $filepath = $merge->open($input)->vague()->mask()->text($text, $text2)->save();
    //     echo '<img src="'.$filepath.'">';
    // }

    private function center_text($text, $fontSize, $imageWidth = 600) {
        $font = $_SERVER['DOCUMENT_ROOT'].'/font/siyuan.ttf';
        $dimensions = imagettfbbox($fontSize, 0, $font, $text);
        return ceil(($imageWidth - $dimensions[4]) / 2);
    }

    public function merge_image($input, $text, $text2){
        $input = $_SERVER['DOCUMENT_ROOT'].$input;
        $font = $_SERVER['DOCUMENT_ROOT'].'/font/siyuan.ttf';
        //获取图片信息
        $info = getimagesize($input);
        $width = $info[0];
        $height = $info[1];
        //获取图片后缀
        $type = image_type_to_extension($info[2], false);
        //创建图片资源句柄
        $func = 'imagecreatefrom'.$type;
        $image = $func($input);

        $alpha = 30;
        $rand = mt_rand(1,4);
        $img = imagecreate($width, $height);
        $img_color = imagecolorallocatealpha($img, 0, 0, 0, 100);
        $img_color = imagecolorallocate($img, 0, 0, 0);
        imagefill($img, 0, 0, $img_color);//填充画布颜色

        for($i=0;$i<5;$i++){
            ImageFilter($image, IMG_FILTER_GAUSSIAN_BLUR, 0, 0, 0);
        }
        ImageFilter($image, IMG_FILTER_SMOOTH, 1);

        imagecopymerge($image, $img, 0, 0, 0, 0, $width, $height, $alpha);

        $text_color = imagecolorallocate($image, 255, 255, 255);
        $fontSize  = 36; //18号字体
        $center_width = $this->center_text($text, $fontSize);
        imagettftext($image, $fontSize, 0, $center_width, $height/2, $text_color, $font, $text);

        $text_color2 = imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
        $fontSize2  = 24; 
        $center_width2 = $this->center_text($text2, $fontSize2);
        imagettftext($image, $fontSize2, 0, $center_width2, $height/2+$fontSize, $text_color, $font, $text2);

        # 5. 保存到文件夹中(默认存放路径为该php文件所在目录)
        $dirpath = '/uploads/merge/'.date('Ymd').'/';
        $filepath = $dirpath.mt_rand(1000,9999).time().'.png';
        $output = $_SERVER['DOCUMENT_ROOT'].$filepath;
        //判断目录是否存在
        if (file_exists($_SERVER['DOCUMENT_ROOT'].$dirpath) == false){
            //创建目录，并且设置最高权限
            mkdir($_SERVER['DOCUMENT_ROOT'].$dirpath, 0700, true);
        }
        imagepng($image, $output);//文件名为GD.png
        # 6. 释放图像(销毁)
        // imagedestroy($img);//释放内存
        imagedestroy($image);

        return $filepath;
    }

    public function merge_image2($input,$input2){
        $input = $_SERVER['DOCUMENT_ROOT'].$input;
        $input2 = $_SERVER['DOCUMENT_ROOT'].$input2;
        // 加载水印以及要加水印的图像
        $stamp = imagecreatefrompng($input2);
        $im = imagecreatefrompng($input);
        // 设置水印图像的外边距，并且获取水印图像的尺寸
        $marge_right = 10;
        $marge_bottom = 10;
        $sx = imagesx($stamp);
        $sy = imagesy($stamp);
        // 利用图像的宽度和水印的外边距计算位置，并且将水印复制到图像上
        imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
        // 输出图像并释放内存
        $dirpath = '/uploads/merge/'.date('Ymd').'/';
        $filepath = $dirpath.mt_rand(1000,9999).time().'.png';
        $output = $_SERVER['DOCUMENT_ROOT'].$filepath;
        //判断目录是否存在
        if (file_exists($_SERVER['DOCUMENT_ROOT'].$dirpath) == false){
            //创建目录，并且设置最高权限
            mkdir($_SERVER['DOCUMENT_ROOT'].$dirpath, 0700, true);
        }
        imagepng($im, $output);
        imagedestroy($stamp);
        imagedestroy($im);
        return $filepath;
    }

    public function merge_image_2($input, $text){
        $input = $_SERVER['DOCUMENT_ROOT'].$input;
        $font = $_SERVER['DOCUMENT_ROOT'].'/font/1571279074.ttf';
        //获取图片信息
        $mark = getimagesize($input);
        //获取图片后缀
        $mark_type = image_type_to_extension($mark[2], false);
        //创建图片资源句柄
        $mark_func = 'imagecreatefrom' . $mark_type;
        $image = $mark_func($input);

        $img_wh = 60;
        $alpha = 60;
        $rand = mt_rand(1,4);
        if($rand < 3){
            $img = imagecreate($mark[0], $img_wh);
        }else{
            $img = imagecreate($img_wh, $mark[1]);
        }
        $color1 = imagecolorallocatealpha($img, 0, 0, 0, 100);
        $color2 = imagecolorallocate($img, 255, 255, 255);
        switch ($rand) {
            case 1:
                imagefill($img, 0, 0, $color1);//填充画布颜色
                imagettftext($img, 16, 0, 20, 35, $color2, $font, $text);
                imagecopymerge($image, $img, 0, $mark[1]-$img_wh, 0, 0, $mark[0], $img_wh, $alpha);
                break;
            case 2:
                imagefill($img, 0, 0, $color1);//填充画布颜色
                imagettftext($img, 16, 0, 20, 35, $color2, $font, $text);
                imagecopymerge($image, $img, 0, 0, 0, 0, $mark[0], $img_wh, $alpha);
                break;
            case 3:
                imagefill($img, 0, 0, $color1);//填充画布颜色
                $chars = preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY);
                $text = implode("\n",$chars);
                imagettftext($img, 16, 0, 20, 55, $color2, $font, $text);
                imagecopymerge($image, $img, $mark[0]-$img_wh, 0, 0, 0, $img_wh, $mark[1], $alpha);
                break;
            case 4:
                imagefill($img, 0, 0, $color1);//填充画布颜色
                $chars = preg_split('//u', $text, -1, PREG_SPLIT_NO_EMPTY);
                $text = implode("\n",$chars);
                imagettftext($img, 16, 0, 20, 55, $color2, $font, $text);
                imagecopymerge($image, $img, 0, 0, 0, 0, $img_wh, $mark[1], $alpha);
                break;
        }

        # 4. 对画布做一些处理(对角线画一条红线)
        // imageline($img, 0,0, 100,100, $color2);

        # 5. 保存到文件夹中(默认存放路径为该php文件所在目录)
        $dirpath = '/uploads/merge/'.date('Ymd').'/';
        $filepath = $dirpath.mt_rand(1000,9999).time().'.png';
        $output = $_SERVER['DOCUMENT_ROOT'].$filepath;
        //判断目录是否存在
        if (file_exists($_SERVER['DOCUMENT_ROOT'].$dirpath) == false){
            //创建目录，并且设置最高权限
            mkdir($_SERVER['DOCUMENT_ROOT'].$dirpath, 0700, true);
        }
        imagepng($image, $output);//文件名为GD.png
        # 6. 释放图像(销毁)
        imagedestroy($img);//释放内存
        imagedestroy($image);

        return $filepath;
    }

    public function test333(){
        //图片水印
        $mark_filename = './uploads/20200318/137b548c5ece2e8efc3dfa54c0b3f7e1.png';
        //获取图片信息
        $info = getimagesize($mark_filename);
        //获取图片的后缀
        $type = image_type_to_extension($info[2], false);
        //拼接图片资源句柄函数
        $func = 'imagecreatefrom' . $type;
        //创建图片资源句柄
        $images = $func($mark_filename);
        //裁剪图片的大小为原图的4分之一
        $w = $info[0] / 4;
        $h = $info[1] / 4;
        //在内存中创建画板
        $mark_image = imagecreatetruecolor($w, $h);
        //裁剪
        imagecopyresampled($mark_image, $images, 0, 0, 0, 0, $w, $h,$info[0],$info[1]);
        //销毁内存
        imagedestroy($images);

        //需要加水印的图片
        $filename = './uploads/pic/20210130/921713dec811c8313be6c9a78b133f5d.png';
        //获取图片信息
        $mark = getimagesize($filename);
        //获取图片后缀
        $mark_type = image_type_to_extension($mark[2], false);
        //创建图片资源句柄
        $mark_func = 'imagecreatefrom' . $mark_type;
        $image = $mark_func($filename);
        //合并，添加水印图片
        imagecopymerge($image, $mark_image, 20, 30, 0, 0, $w, $h, 50);
        //销毁内存
        imagedestroy($mark_image);

        //文字水印
        $font = $_SERVER['DOCUMENT_ROOT'].'/ttf/AaJieMoDun.ttf';
        $content = "this 文字";
        //设置字体的颜色及透明图
        // $col = imagecolorallocatealpha($image, 45, 56, 123, 127);
        //将文字写入图片
        imagettftext($image, 30, 30, 300, 200, 255, $font, $content);


        //图片输出函数拼接
        $outFunc = 'image'.$type;
        //设置文件路径
        $output = './test.png';//存储的路径
        //判断目录是否存在
        // if (file_exists($ml) == false){
        //     //创建目录，并且设置最高权限
        //     if (mkdir($ml, 0700, true)){
        //         echo '创建目录成功';
        //     }else{
        //         echo '创建目录失败';
        //     }
        // }

        //生成图片
        if ($outFunc($image,$output)){
            echo '<img src="/test.png">';
        }else{
            var_dump('压缩图片失败');
        }
        

    }

    public function tes1t(){
        $font = $_SERVER['DOCUMENT_ROOT'].'/font/AaJieMoDun.ttf';
        $input = './uploads/pic/20210130/921713dec811c8313be6c9a78b133f5d.png';
        // 加载要加水印的图像
        $im = imagecreatefrompng($input);

        // 首先我们从 GD 手动创建水印图像
        $stamp = imagecreatetruecolor(100, 70);
        imagefilledrectangle($stamp, 0, 0, 99, 69, 0x0000FF);
        imagefilledrectangle($stamp, 9, 9, 90, 60, 0xFFFFFF);
        imagestring($stamp, 5, 20, 20, 'aaabbb', 0x0000FF);
        imagestring($stamp, 3, 20, 40, '(c) 2007-9', 0x0000FF);

        // 设置水印图像的位置和大小
        $marge_right = 10;
        $marge_bottom = 10;
        $sx = imagesx($stamp);
        $sy = imagesy($stamp);

        // 以 50% 的透明度合并水印和图像
        imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);

        // 将图像保存到文件，并释放内存
        imagepng($im, "./test.png");
        imagedestroy($im);
        echo '<img src="/test.png">';
    }

    public function test2233(){
        $input = './uploads/pic/20210130/921713dec811c8313be6c9a78b133f5d.png';
        $input2 = './uploads/20200318/137b548c5ece2e8efc3dfa54c0b3f7e1.png';
        // 加载水印以及要加水印的图像
        $stamp = imagecreatefrompng($input2);
        $im = imagecreatefrompng($input);

        // 设置水印图像的外边距，并且获取水印图像的尺寸
        $marge_right = 10;
        $marge_bottom = 10;
        $sx = imagesx($stamp);
        $sy = imagesy($stamp);


        // 利用图像的宽度和水印的外边距计算位置，并且将水印复制到图像上

        imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

        // 输出图像并释放内存
        imagepng($im, "./test.png");
        imagedestroy($im);
        echo '<img src="/test.png">';
    }

    public function test223()
    {
        $ttf = $_SERVER['DOCUMENT_ROOT'].'/ttf/AaJieMoDun.ttf';
        $this->editor = Grafika::createEditor();
        $input1 = './uploads/pic/20210130/921713dec811c8313be6c9a78b133f5d.png';
        $output = './test.png';
        // $this->hunhe();
        // $this->wenzi();
        // $this->juxing();
        echo '<img src="/test.png">';
        $result = $this->editor->compare($input1, $output); //图片相似度
        var_dump($result);
    }

    public function hunhe(){
        $input = './uploads/pic/20210130/921713dec811c8313be6c9a78b133f5d.png';
        $input2 = './uploads/20200318/137b548c5ece2e8efc3dfa54c0b3f7e1.png';
        $output = './test.png';

        $this->editor->open( $image, $input );
        $this->editor->open( $image2, $input2 );
        $this->editor->blend( $image, $image2, 'overlay', 0.5, 'center' ); // overlay blend, opacity 50%, center position
        $this->editor->save( $image, $output );
    }

    public function wenzi(){
        $input = './uploads/pic/20210130/921713dec811c8313be6c9a78b133f5d.png';
        $output = './test.png';
        $font = $_SERVER['DOCUMENT_ROOT'].'/ttf/AaJieMoDun.ttf';
        $this->editor->open( $image, $input );
        $this->editor->text( $image, '一点点2', 30, 200, 200, new Color('#ff0033', 0.5), $font, 10);
        $this->editor->save( $image, $output, null, 100, true);
    }

    public function juxing(){
        $input = './uploads/pic/20210130/921713dec811c8313be6c9a78b133f5d.png';
        $output = './test.png';
        $num = 70;
        // $this->editor->draw( $image, Grafika::createDrawingObject('Rectangle', $num, $height, array(0, 0), 0, null, new Color('#ffffff') ));
        // $this->editor->draw( $image, Grafika::createDrawingObject('Rectangle', $num, $height, array($width - $num, 0), 0, null, new Color('#ffffff') ));
        // $this->editor->draw( $image, Grafika::createDrawingObject('Rectangle', $width, $num, array(0, 0), 0, null, new Color('#ffffff') ));
        $this->editor->open( $image, $input );
        $this->editor->draw( $image, Grafika::createDrawingObject('Rectangle', $width, $num, array(0, $height - $num), 0, null, new Color('#ffffff', 0.5) ));
        $this->editor->save( $image, $output, null, 100, true);
    }



    public function test3()
    {
        $font = $_SERVER['DOCUMENT_ROOT'].'/ttf/AaJieMoDun.ttf';
        $input = './uploads/pic/20210130/921713dec811c8313be6c9a78b133f5d.png';
        $output = './test.png';
        Image::open($input)->text('ceshiddd',$font,12,'#ffffff')->save($output);
        echo '<img src="/test.png">';
    }
    
    public function test2()
    {
        $t = new ThumbHandler();
        $t->setSrcImg("./uploads/pic/20210130/921713dec811c8313be6c9a78b133f5d.png");
        $t->setMaskWord("test");
        $t->setDstImgBorder(10,"#dddddd");
        $t->setDstImg("./test.png");
        $t->createImg();
        echo '<img src="/test.png">';
    }

    public function testfda()
    {
        $image_path = './uploads/a.png';
        $editor = Grafika::createEditor();
        $editor->open($image , $image_path);
        $height = $image->getHeight(); //高
        $width = $image->getWidth(); //宽
        $type = $image->getType(); //类型
        $file_path = './test.jpg';
        
        $result = $editor->compare('./uploads/merge/20210416/59361618568670.png', './uploads/merge/20210416/34881618568269.png'); //图片相似度
        var_dump($result);
        die;
        // $isAnima = $image->isAnimated(); //是否是动图
        
        // $editor->resizeFit($image , 200 , 200); //缩略
        // $editor->crop( $image, 200, 200, 'smart' ); //裁剪
        // $editor->text($image ,'ceshi ceshi',30,200,100,new Color("#f32343"),'',45);//文字
        // $filter = Grafika::createFilter('Blur', 50); // 模糊度为10，模糊度取值为0-100
        // $filter = Grafika::createFilter('Brightness', 50); //亮度
        // $filter = Grafika::createFilter('Colorize', -50,50,-50);//颜色
        // $filter = Grafika::createFilter('Contrast', 50);//对比度
        // $filter = Grafika::createFilter('Dither', 'diffusion'); //噪点
        // $filter = Grafika::createFilter('Gamma', 2.0); //色阶
        // $filter = Grafika::createFilter('Grayscale'); //灰度
        // $filter = Grafika::createFilter('Invert'); //反色
        // $filter = Grafika::createFilter('Pixelate',10); //栅格化
        // $filter = Grafika::createFilter('Sharpen',50); //锐化
        // $filter = Grafika::createFilter('Sobel'); //边缘化
        // $editor->apply( $image, $filter ); // 将滤镜应用到图片

        //绘制贝塞尔曲线
        // $editorName = Grafika::detectAvailableEditor();
        // if('Imagick'===$editorName){
        //     $drawingObject = new ImagickCubicBezier(array(42, 180), array(230, 190), array(42, 45), array(300, 43), '#ff0000');
        // } else if ('Gd'===$editorName) {
        //     $drawingObject = new GdCubicBezier(array(42, 180), array(230, 190), array(42, 45), array(300, 43), '#ff0000');
        // }
        // $editor->draw( $image, $drawingObject );
        //绘制椭圆
        // $drawingObject = Grafika::createDrawingObject('Ellipse', 200, 100, array(50, 75), 1, new Color('#ff0000'), new Color('#FF0000'));
        // $editor->draw( $image, $drawingObject );
        //绘制直线
        // $editor->draw($image, Grafika::createDrawingObject('Line', array(0, 0), array(200, 200), 1, new Color('#FF0000')));
        // $editor->draw($image, Grafika::createDrawingObject('Line', array(0, 200), array(200, 0), 1, new Color('#00FF00')));
        // $editor->draw($image, Grafika::createDrawingObject('Line', array(0, 0), array(200, 100), 1, new Color('#0000FF')));
        // $editor->draw($image, Grafika::createDrawingObject('Line', array(0, 100), array(200, 100)));
        // $editor->draw($image, Grafika::createDrawingObject('Line', array(100, 0), array(100, 200)));
        //绘制多边形
        // $editor->draw( $image, Grafika::createDrawingObject('Polygon', array(array(0,0), array(50,0), array(0,50)), 1));
        // $editor->draw( $image, Grafika::createDrawingObject('Polygon', array(array(200-1,0), array(150-1,0), array(200-1,50)), 1));
        // $editor->draw( $image, Grafika::createDrawingObject('Polygon', array(array(100,0), array(140,50), array(100,100), array(60,50)), 1, null, new Color('#FF0000')));
        //二次贝塞尔曲线
        // $drawingObject = Grafika::createDrawingObject('QuadraticBezier', array(70, 250), array(20, 110), array(220, 60), '#FF0000');
        // $editor->draw( $image, $drawingObject );
        //绘制矩形
        $num = 70;
        // $editor->draw( $image, Grafika::createDrawingObject('Rectangle', $num, $height, array(0, 0), 0, null, new Color('#ffffff') ));
        // $editor->draw( $image, Grafika::createDrawingObject('Rectangle', $num, $height, array($width - $num, 0), 0, null, new Color('#ffffff') ));
        // $editor->draw( $image, Grafika::createDrawingObject('Rectangle', $width, $num, array(0, 0), 0, null, new Color('#ffffff') ));
        $editor->draw( $image, Grafika::createDrawingObject('Rectangle', $width, $num, array(0, $height - $num), 0, null, new Color('#ffffff', 0.5) ));
        $font = $_SERVER['DOCUMENT_ROOT'].'/font/AaJieMoDun.ttf';
        $editor->text( $image, '一点点4', 30, 200, 200, new Color('#ff0033', 1), $font, 10);
        // $editor->draw( $image, Grafika::createBlankImage(100,100));
        $editor->save( $image, $file_path );
        echo '<img src="/test.jpg">';
        $result = $editor->compare($image_path, $file_path); //图片相似度
        var_dump($result);
        

        
    }

    
    

}